package com.zpark;

import com.zpark.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

@WebServlet("/sd01.do")
public class ServletDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 将数据绑定到请求域对象当中，然后转发到demo02.jsp页面做显示
         */
        User user = new User();
        user.setUsername("大锤");
        user.setPassword("123456");
        user.setAge(23);

        // 将user对象绑定到request域当中
        request.setAttribute("user", user);

        Map<String, Object> map = new HashMap<>();
        map.put("userData", user);
        map.put("time", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date()));
        map.put("array", new int[]{1, 2, 3, 4});
        map.put("str", "map集合");

        // 将map对象绑定到request域
        request.setAttribute("myMap", map);

        String[] strs = new String[3];
        strs[0] = "张三";
        strs[1] = "李四";
        strs[2] = "王五";

        request.setAttribute("myArrays", strs);

        LinkedList<Object> ll = new LinkedList<>();
        ll.add("大锤-list集合");
        ll.add(user);
        ll.add(map);

        request.setAttribute("lists", ll);

        request.setAttribute("pId", "132as1fdf454fdsf21fds");


        System.out.println(request.getContextPath() + "/jsp/demo02.jsp");
        // 转发到demo02.jsp页面显示
        request.getRequestDispatcher("./jsp/demo02.jsp").forward(request, response);

    }
}
